/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;

import ru.irenproject.TestProtos.AvailableScore;
import ru.irenproject.modifier.ShuffleChoicesModifier;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;
import ru.irenproject.store.Store;

public final class Profile extends AbstractThing {
  public enum ChangeTitleEvent implements Event { INSTANCE }
  public enum ChangeEvent implements Event { INSTANCE }

  public static Profile createDefault(Store store) {
    Profile res = store.create(Profile.class);
    res.setDefaults();
    return res;
  }

  @Stash private TestProtos.Profile.Builder $;

  @Inject Profile() {}

  @Override protected void initialize() {
    $.setTitle("")
     .setShuffleQuestions(false)
     .setEditableAnswers(false)
     .setBrowsableQuestions(false)
     .setWeightCues(false)
     .setInstantAnswerCorrectness(false)
     .setInstantTotalPercentCorrect(false)
     .setMarkScale(MarkScale.EMPTY.message())
     .setModifierList(store().create(ModifierList.class).id());
  }

  public ModifierList modifierList() {
    return get($.getModifierList(), ModifierList.class);
  }

  public String title() {
    return $.getTitle();
  }

  public void setTitle(String value) {
    $.setTitle(value);
    post(ChangeTitleEvent.INSTANCE);
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasQuestionsPerSection() {
    return $.hasQuestionsPerSection();
  }

  public int questionsPerSection() {
    Validate.isTrue(hasQuestionsPerSection());
    return $.getQuestionsPerSection();
  }

  public void setQuestionsPerSection(int value) {
    TestCheck.input(value >= 1);
    $.setQuestionsPerSection(value);
    post(ChangeEvent.INSTANCE);
  }

  public void clearQuestionsPerSection() {
    $.clearQuestionsPerSection();
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasLabelFilter() {
    return $.hasLabelFilter();
  }

  public String labelFilter() {
    Validate.isTrue(hasLabelFilter());
    return $.getLabelFilter();
  }

  public void setLabelFilter(String value) {
    $.setLabelFilter(value);
    post(ChangeEvent.INSTANCE);
  }

  public void clearLabelFilter() {
    $.clearLabelFilter();
    post(ChangeEvent.INSTANCE);
  }

  public boolean shuffleQuestions() {
    return $.getShuffleQuestions();
  }

  public void setShuffleQuestions(boolean value) {
    $.setShuffleQuestions(value);
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasDurationMinutes() {
    return $.hasDurationMinutes();
  }

  public int durationMinutes() {
    Validate.isTrue(hasDurationMinutes());
    return $.getDurationMinutes();
  }

  public void setDurationMinutes(int value) {
    TestCheck.input(value >= 1);
    $.setDurationMinutes(value);
    post(ChangeEvent.INSTANCE);
  }

  public void clearDurationMinutes() {
    $.clearDurationMinutes();
    post(ChangeEvent.INSTANCE);
  }

  public boolean editableAnswers() {
    return $.getEditableAnswers();
  }

  public void setEditableAnswers(boolean value) {
    $.setEditableAnswers(value);
    post(ChangeEvent.INSTANCE);
  }

  public boolean browsableQuestions() {
    return $.getBrowsableQuestions();
  }

  public void setBrowsableQuestions(boolean value) {
    $.setBrowsableQuestions(value);
    post(ChangeEvent.INSTANCE);
  }

  public boolean weightCues() {
    return $.getWeightCues();
  }

  public void setWeightCues(boolean value) {
    $.setWeightCues(value);
    post(ChangeEvent.INSTANCE);
  }

  public boolean instantAnswerCorrectness() {
    return $.getInstantAnswerCorrectness();
  }

  public void setInstantAnswerCorrectness(boolean value) {
    $.setInstantAnswerCorrectness(value);
    post(ChangeEvent.INSTANCE);
  }

  public boolean instantTotalPercentCorrect() {
    return $.getInstantTotalPercentCorrect();
  }

  public void setInstantTotalPercentCorrect(boolean value) {
    $.setInstantTotalPercentCorrect(value);
    post(ChangeEvent.INSTANCE);
  }

  public boolean hasAvailableScore() {
    return $.hasAvailableScore();
  }

  public AvailableScore availableScore() {
    Validate.isTrue(hasAvailableScore());
    return $.getAvailableScore();
  }

  public void setAvailableScore(AvailableScore value) {
    Validate.isTrue(value.isInitialized());
    $.setAvailableScore(value);
    post(ChangeEvent.INSTANCE);
  }

  public void clearAvailableScore() {
    $.clearAvailableScore();
    post(ChangeEvent.INSTANCE);
  }

  public MarkScale markScale() {
    return new MarkScale($.getMarkScale());
  }

  public void setMarkScale(MarkScale value) {
    $.setMarkScale(value.message());
    post(ChangeEvent.INSTANCE);
  }

  private void setDefaults() {
    $.setShuffleQuestions(true)
     .setEditableAnswers(true)
     .setBrowsableQuestions(true)
     .setAvailableScore(AvailableScore.newBuilder()
         .setPercentCorrect(true)
         .setPoints(false)
         .setMark(true)
         .build());
    modifierList().add(store().create(ShuffleChoicesModifier.class));
  }
}
